package com.hao.tool.web.module.fileInfo.controller;

import com.hao.tool.common.enums.ShortEnum;
import com.hao.tool.web.module.base.entity.ShortInfo;
import com.hao.tool.web.module.base.service.ShortInfoService;
import com.hao.tool.web.module.fileInfo.entity.FileInfo;
import com.hao.tool.web.module.fileInfo.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 21:46
 */
@Controller
@RequestMapping("/file")
public class FileInfoController {
    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private ShortInfoService shortInfoService;

    @GetMapping("")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("fileinfo/index");
        return mv;
    }

    @GetMapping("/info/{fileId}")
    public ModelAndView info(@PathVariable String fileId) {
        if ("0".equals(fileId)) {
            fileId = fileInfoService.fineMaxId();
        }
        FileInfo fileInfo = fileInfoService.getById(fileId);
        ModelAndView mv = new ModelAndView("fileinfo/info");

        if (fileInfo == null) {
            mv.setViewName("common/404");
        } else {
            mv.addObject("fileInfo", fileInfo);
        }

        ShortInfo shortInfo = shortInfoService.find(ShortEnum.FILE, fileId);
        if (shortInfo != null) {
            mv.addObject("shortId", shortInfo.getId());
        } else {
            mv.addObject("shortId", "无");
        }

        return mv;
    }


    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("fileinfo/add");
        mv.addObject("storageType", "ftp");
        return mv;
    }

    @GetMapping("/add/{storageType}")
    public ModelAndView cos(@PathVariable String storageType) {
        ModelAndView mv = new ModelAndView("fileinfo/addcos");
        mv.addObject("storageType", storageType);
        return mv;
    }

    @GetMapping("/add/img")
    public ModelAndView addImage() {
        ModelAndView mv = new ModelAndView("fileinfo/addimg");
        mv.addObject("storageType", "local");
        return mv;
    }
}
